<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {
 
 
    function __construct()
    {
         parent::__construct();
    }


	//登录操作
	function login($username,$password,$isemail=false)
	{

		if($isemail)
		{
			$this->db->where('email',$username);
		}
		else
		{
			$this->db->where('username',$username);
		}

		$query = $this->db->get('user');
		
		//echo $this->db->last_query();
		$pwdmd5 = md5($password);

		if (($row = $query->row_array()))
		{
			if($row['password']==$pwdmd5)
			{
				return $row;
			}
			else
			{
				return false;
			}
		}
		return false;

	}

	function select_user_page($data = array(
			'username'=>''  //用户名
			,'email'=>''		//邮箱名称
			,'roleid'=>0		//角色id
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		if($data['username'])
		{
			$this->db->like('username',$data['username']);
		}
		
		if($data['email'])
		{
			$this->db->like('email',$data['email']);
		}
		if($data['roleid'])
		{
			$this->db->where('roleid',$data['roleid']);
		}

	    $this->db->from('user');

		//查询总记录数
		$count = $this->db->count_all_results();

		$user['count'] = $count;
		/////////////////////////////////////////////////////////////////
		if($data['username'])
		{
			$this->db->like('username',$data['username']);
		}
		
		if($data['email'])
		{
			$this->db->like('email',$data['email']);
		}
		if($data['roleid'])
		{
			$this->db->where('roleid',$data['roleid']);
		}

	    $this->db->from('user');
		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		
		$query = $this->db->get();

		$user['list']=$query->result_array();

		return $user;
	}


	function select_user_by_id($uid)
	{
		$this->db->where('uid',$uid);
		$query = $this->db->get('user');
		$row = $query->row_array();
		return $row;
	}

	function update_user($uid,$data=array())
	{
		$fields = array();
		if($data['username'])
		{
			$fields['username'] = $data['username'];
		}
		if($data['email'])
		{
			$fields['email'] = $data['email'];
		}
		if($data['password'])
		{
			$fields['password'] = md5($data['password']);
		}
		$this->db->where('uid', $uid);
		return $this->db->update('user', $fields); 
	}

	function insert_user($data=array())
	{
		$fields = array();
		if($data['username'])
		{
			$fields['username'] = $data['username'];
		}
		if($data['email'])
		{
			$fields['email'] = $data['email'];
		}
		if($data['regdate'])
		{
			$fields['regdate'] = $data['regdate'];
		}
		if($data['password'])
		{
			$fields['password'] = md5($data['password']);
		}
		$result = $this->db->insert('user', $fields); 
		
		$data['uid'] = $this->db->insert_id();
		
		$data['result'] =$result;
		
		return $data;
	}

	function delete_user($uid)
	{
		$result =array('success'=>true);
		$this->db->where('uid',$uid);
		$query = $this->db->get('user');
		$row = $query->row_array();
		if($row && $row['roleid']>=8)
		{
			$result['success']=false;
			$result['message'] ='删除用户信息失败，当前用户是管理员，不能删除！';
			return $result;
		}
		$this->db->where('uid',$uid);
		$result['data'] = $this->db->delete('user'); 
		return $result;
	}

	
	//唯一性检查
	function check_unique($username,$email,$uid=0)
	{
		$sql = "select * from ".$this->db->dbprefix('user')." where (username='$username' OR email='$email')";
		if($uid)
		{
			$sql .="uid !='$uid'";
		}
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if($row)
		{
				if($row['username']==$username)
				{
					return -1;
				}
				if($row['email']==$email)
				{
					return -2;
				}
				return false;
		}
		return true;
	}
}